const fs = require('fs');
const path = require('path');
const os = require('os');
const { execSync } = require('child_process');

if (os.platform() === 'win32') {
    const HOST_NAME = 'com.ide.nativehost';
    const manifestPath = path.join(__dirname, 'manifest.json');
    const manifest = {
        "name": HOST_NAME,
        "description": "rpa_ide清单",
        "path": path.join(__dirname, 'chrome_native_messaging_host.exe'),
        "type": "stdio",
        "allowed_origins": [
            "chrome-extension://gcbalfbdmfieckjlnblleoemohcganoc/"
        ]
    }
    fs.writeFileSync(manifestPath, JSON.stringify(manifest, undefined, 2))
    const registryKey = `HKCU\\Software\\Google\\Chrome\\NativeMessagingHosts\\${HOST_NAME}`;
    const escapedPath = manifestPath.replace(/\\/g, '\\\\');
    const regCommand = `reg add "${registryKey}" /ve /t REG_SZ /d "${escapedPath}" /f`;
    execSync(regCommand, { stdio: 'pipe' });
}

